from .CreateWindow import CreateWindow

from event import Event

from widgets.ScriptItem import ScriptItem
from PySide6.QtCore import QRect, Qt
from PySide6.QtGui import (QAction, QFont, QKeySequence)
from PySide6.QtWidgets import *
from config import ScriptList


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setObjectName(u"MainWindow")
        self.setFixedSize(700, 500)
        self._createWindow = None
        self.setWindowTitle('km-machine')

        self.initUI()

    def initUI(self):
        self.actionNew = QAction(self)
        self.actionNew.setObjectName(u"actionNew")
        self.actionNew.setShortcuts(QKeySequence.StandardKey.New)
        self.actionNew.triggered.connect(self.onNewAction)
        self.actionNew.setText("新建脚本")

        self.centralwidget = QWidget(self)
        self.centralwidget.setObjectName(u"centralwidget")

        self.labelTitle = QLabel(self.centralwidget)
        self.labelTitle.setObjectName(u"labelTitle")
        self.labelTitle.setGeometry(QRect(275, 10, 200, 40))
        font = QFont()
        font.setPointSize(20)
        self.labelTitle.setFont(font)
        self.labelTitle.setAlignment(Qt.AlignCenter)
        self.labelTitle.setText("脚本列表")

        self.labelHeaderName = QLabel(self.centralwidget)
        self.labelHeaderName.setObjectName(u"labelHeaderName")
        self.labelHeaderName.setGeometry(QRect(40, 60, 71, 16))
        self.labelHeaderName.setAlignment(Qt.AlignCenter)
        self.labelHeaderName.setText("名称")

        self.name = QLabel(self.centralwidget)
        self.name.setObjectName(u"name")
        self.name.setGeometry(QRect(190, 60, 58, 16))
        self.name.setAlignment(Qt.AlignCenter)
        self.name.setText("描述")

        self.labelHeaderCreatedAt = QLabel(self.centralwidget)
        self.labelHeaderCreatedAt.setObjectName(u"labelHeaderCreatedAt")
        self.labelHeaderCreatedAt.setGeometry(QRect(310, 60, 58, 16))
        self.labelHeaderCreatedAt.setAlignment(Qt.AlignCenter)
        self.labelHeaderCreatedAt.setText("创建时间")

        self.labelHeaderUpdatedAt = QLabel(self.centralwidget)
        self.labelHeaderUpdatedAt.setObjectName(u"labelHeaderUpdatedAt")
        self.labelHeaderUpdatedAt.setGeometry(QRect(400, 60, 58, 16))
        self.labelHeaderUpdatedAt.setAlignment(Qt.AlignCenter)
        self.labelHeaderUpdatedAt.setText("更新时间")

        self.labelHeaderOperate = QLabel(self.centralwidget)
        self.labelHeaderOperate.setObjectName(u"labelHeaderOperate")
        self.labelHeaderOperate.setGeometry(QRect(550, 60, 58, 16))
        self.labelHeaderOperate.setAlignment(Qt.AlignCenter)
        self.labelHeaderOperate.setText("操作")

        self.scrollArea = QScrollArea(self.centralwidget)
        self.scrollArea.setObjectName(u"scrollArea")
        self.scrollArea.setGeometry(QRect(0, 80, 750, 371))
        self.scrollArea.setWidgetResizable(True)

        self.setCentralWidget(self.centralwidget)

        self.menubar = QMenuBar(self)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 750, 24))

        self.menu = QMenu(self.menubar)
        self.menu.setObjectName(u"menu")
        self.menu.setTitle("文件")

        self.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(self)
        self.statusbar.setObjectName(u"statusbar")
        self.setStatusBar(self.statusbar)

        self.menubar.addAction(self.menu.menuAction())
        self.menu.addAction(self.actionNew)

        self.initScriptList()
        Event.addListener('script_add', self.addScript)

    def onNewAction(self):
        if not self._createWindow and self._createWindow is None:
            self._createWindow = CreateWindow(self)

        self._createWindow.show()

    def initScriptList(self):
        self.scriptListWidget = QWidget(self)
        self.scriptListLayout = QVBoxLayout()
        self.scriptListWidget.setLayout(self.scriptListLayout)
        self.scrollArea.setWidget(self.scriptListWidget)
        self.refreshScriptList()

    def refreshScriptList(self):
        self.scriptListLayout.setAlignment(Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft)
        for script in ScriptList.scriptList:
            scriptWidget = ScriptItem(script)
            self.scriptListLayout.addWidget(scriptWidget)

    def clearScriptList(self):
        itemList = list(range(self.scriptListLayout.count()))
        itemList.reverse()
        for i in itemList:
            item = self.scriptListLayout.itemAt(i)
            self.scriptListLayout.removeItem(item)
            if item.widget():
                item.widget().deleteLater()

    def addScript(self, script):
        scriptWidget = ScriptItem(script)
        self.scriptListLayout.addWidget(scriptWidget)
